document.addEventListener("DOMContentLoaded", function() {
    const audio = document.getElementById("bgMusic");

    // 从localStorage中读取音乐播放状态
    const musicState = JSON.parse(localStorage.getItem("musicState") || "{}");

    // 如果之前有播放状态，则恢复
    if (musicState.isPlaying) {
        audio.currentTime = musicState.currentTime;
        audio.play();
    }

    // 监听音频播放事件，保存状态到localStorage
    audio.addEventListener("timeupdate", function() {
        localStorage.setItem("musicState", JSON.stringify({
            isPlaying: !audio.paused,
            currentTime: audio.currentTime
        }));
    });

    audio.addEventListener("pause", function() {
        localStorage.setItem("musicState", JSON.stringify({
            isPlaying: false,
            currentTime: audio.currentTime
        }));
    });
});